package cn.net.vote.util;

import java.awt.Color;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFFont;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.util.HSSFColor;
import org.springframework.web.servlet.view.document.AbstractExcelView;

import cn.net.vote.data.domain.PageData;


/**
* 导入到EXCEL
* 类名称：ObjectExcelView.java
* @author Tianxy
* @version 1.0
 */
public class CheckPointStatisticsExcelView extends AbstractExcelView{

	@Override
	protected void buildExcelDocument(Map<String, Object> model,
			HSSFWorkbook workbook, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		// TODO Auto-generated method stub
		Date date = new Date();
		String filename = Tools.date2Str(date, "yyyyMMddHHmmss");
		HSSFSheet sheet;
		HSSFCell cell;
		response.setContentType("application/octet-stream");
		response.setHeader("Content-Disposition", "attachment;filename="+filename+".xls");
		sheet = workbook.createSheet("sheet1");
		
		 Set<String> titles = (Set<String>) model.get("titles");
		int len = titles.size();
		HSSFCellStyle headerStyle = workbook.createCellStyle(); //标题样式
		headerStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);
		headerStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
		HSSFCellStyle redHeaderStyle = workbook.createCellStyle(); //标题样式
		redHeaderStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);
		redHeaderStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
		HSSFFont redHeaderFont = workbook.createFont();	//标题字体
		redHeaderFont.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
		redHeaderFont.setFontHeightInPoints((short)11);
		redHeaderFont.setColor(HSSFColor.RED.index);
		redHeaderStyle.setFont(redHeaderFont);
		HSSFFont headerFont = workbook.createFont();	//标题字体
		headerFont.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
		headerFont.setFontHeightInPoints((short)11);
		headerStyle.setFont(headerFont);
		short width = 17,height=25*20;
		sheet.setDefaultColumnWidth(width);
		  int i = 0;
		for(Iterator<String> it = titles.iterator() ;it.hasNext();){
			
			String title = it.next();
			cell = getCell(sheet, 0, i);
			if(title.contains("*")){
				cell.setCellStyle(redHeaderStyle);
			}else{
				cell.setCellStyle(headerStyle);	
			}
			
			setText(cell,title);
			i++;
		}
		
		 
		sheet.getRow(0).setHeight(height);
		
		HSSFCellStyle contentStyle = workbook.createCellStyle(); //内容样式
		contentStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);
		List<PageData> varList = (List<PageData>) model.get("varList");
		int varCount = varList.size();
	 
		
		for(int k=0;k<varCount; k++){
			PageData vpd = varList.get(k);
			 
			int j = 0;
			for(Iterator<String> it = titles.iterator();it.hasNext();){
				
				String key = it.next();
				String varstr = vpd.get(key) != null ? String.valueOf(vpd.get(key)) : "";
				cell = getCell(sheet, k+1, j);
				cell.setCellStyle(contentStyle);
				setText(cell,varstr);
				j++;
			}
			
			
			
			 
			
		}
		
	}

}
